﻿<template>
  <van-popup v-model="visible" position="bottom" closeable>
    <van-form @submit="handleSubmit" :loading="loading">
      <van-field
        v-model="formFields.TaskName"
        name="TaskName"
        label="作业名称"
        :rules="[{ required: true, message: '必填' }]"
      />

      <van-field
        v-model="formFields.GroupName"
        name="GroupName"
        label="分组"
        :rules="[{ required: true, message: '必填' }]"
      />

      <van-field
        v-model="formFields.Interval"
        name="Interval"
        label="间隔(Cron)"
        :rules="[{ required: true, message: '必填' }]"
      />

      <van-field
        v-model="formFields.ApiUrl"
        name="ApiUrl"
        label="ApiUrl"
        :rules="[{ required: true, message: '必填' }]"
      />

      <van-field
        v-model="formFields.AuthKey"
        name="AuthKey"
        label="Header(key)"
        :rules="[{ required: true, message: '必填' }]"
      />

      <van-field
        v-model="formFields.AuthValue"
        name="AuthValue"
        label="Header(value)"
        :rules="[{ required: true, message: '必填' }]"
      />

      <van-field
        v-model="formFields.RequestType"
        name="RequestType"
        label="请求方式"
        :rules="[{ required: true, message: '必填' }]"
      />

      <van-field
        v-model="formFields.Describe"
        name="Describe"
        label="描述"
        :rules="[{ required: true, message: '必填' }]"
      />

      <div style="margin: 16px;">
        <van-button block type="info" native-type="submit">提交</van-button>
      </div>
    </van-form>
  </van-popup>
</template>

<script>
import {
  Quartz_Task_GetTheData,
  Quartz_Task_SaveData
} from "@/api/Quartz_Manage/Quartz_Task";

export default {
  props: {
    afterSubmit: {
      type: Function,
      default: null
    }
  },
  data() {
    return {
      visible: false,
      loading: false,
      formFields: {
        TaskName: "",
        GroupName: "",
        Interval: "",
        ApiUrl: "",
        AuthKey: "",
        AuthValue: "",
        Describe: "",
        RequestType: ""
      },
      entity: {}
    };
  },
  methods: {
    add() {
      this.entity = {};
      this.visible = true;
      this.formFields = {
        TaskName: "",
        GroupName: "",
        Interval: "",
        ApiUrl: "",
        AuthKey: "",
        AuthValue: "",
        Describe: "",
        RequestType: ""
      };
    },
    edit(id) {
      this.visible = true;
      this.$nextTick(() => {
        this.formFields = {
          TaskName: "",
          GroupName: "",
          Interval: "",
          ApiUrl: "",
          AuthKey: "",
          AuthValue: "",
          Describe: "",
          RequestType: ""
        };

        Quartz_Task_GetTheData({ id: id }).then(resJson => {
          this.entity = resJson.Data;
          Object.keys(this.formFields).forEach(item => {
            this.formFields[item] = this.entity[item];
          });
        });
      });
    },

    handleSubmit(value) {
      this.entity = Object.assign(this.entity, this.formFields);
      this.loading = true;
      Quartz_Task_SaveData(this.entity).then(resJson => {
        this.loading = false;

        if (resJson.Success) {
          this.$toast("操作成功");
          this.afterSubmit();
          this.visible = false;
        } else {
          this.$toast(resJson.Msg);
        }
      });
    }
  }
};
</script>
